package com.tiantianyuegou.view.refresh;

import android.content.Context;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import com.tiantianyuegou.R;

/**
 * 
 *@Title:下拉刷新view
 *@Description:
 *@Author:trouble
 *@Since:2015-9-3
 *@Version:1.1.0
 */
public class PullToRefreshLayout extends SwipeRefreshLayout {

	private float mInitialMotionX;
	private float mEdgeSlop;

	public PullToRefreshLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		ViewConfiguration config = ViewConfiguration.get(context);
		mEdgeSlop = config.getScaledEdgeSlop();
		setColorSchemeColors(context.getResources().getColor(R.color.style_color_primary));
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		switch (MotionEventCompat.getActionMasked(ev)) {
		case MotionEvent.ACTION_DOWN: {
			mInitialMotionX = ev.getX();
			break;
		}

		case MotionEvent.ACTION_MOVE:
			final float x = ev.getX();
			if (Math.abs(x - mInitialMotionX) > mEdgeSlop) {
				MotionEvent cancelEvent = MotionEvent.obtain(ev);
				cancelEvent.setAction(MotionEvent.ACTION_CANCEL);
				return super.onInterceptTouchEvent(cancelEvent);
			}
		}
		return super.onInterceptTouchEvent(ev);
	}
}
